package ps.client.plugin.eq2.gui.dialog.chat;

import java.awt.Color;
import java.awt.Font;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import ps.client.gui.util.textpanel.ChatLineFormater;
import ps.client.gui.util.textpanel.TextLine;
import ps.client.plugin.eq2.log.ChatLink;
import ps.client.plugin.eq2.log.TriggerImpl;


public class Eq2ChatLineFormater implements ChatLineFormater {
    
    public static final Font FONT_TIME_STAMP = new Font("Courier New", Font.PLAIN, 11);
    public static final Font FONT_SENDER = new Font("Arial", Font.PLAIN, 12);
    public static final Font FONT_MESSAGE_1 = new Font("Arial", Font.PLAIN, 12);
    public static final Font FONT_MESSAGE_2 = new Font("Arial", Font.PLAIN, 12);
    
    public static final Color COLOR_TIME_STAMP = Color.GRAY;
    public static final Color COLOR_SENDER = new Color(100, 255, 150);
    public static final Color COLOR_RECIEVER = new Color(100, 255, 150);
    
    public static final Color COLOR_MESSAGE_1 = new Color(255, 255, 255);
    public static final Color COLOR_MESSAGE_2 = new Color(0, 255, 0);
    
    public static final Color COLOR_BACKGROUND = Color.BLACK;
    
    public static final Color COLOR_WEB_LINK = new Color(200, 255, 200);
    public static final Color COLOR_ITEM_LINK = new Color(200, 255, 200);
    
    static final Pattern HTTP_LINK_PATTERN = Pattern.compile(TriggerImpl.get(TriggerImpl.HTTP_LINK));
    static final Pattern ITEM_LINK_PATTERN = Pattern.compile(TriggerImpl.get(TriggerImpl.ITEM_LINK));
    
    boolean timeStamp = true;
    
    @Override
    public TextLine formatChatLine(String sender, String reciever, String msg) {
        TextLine textLine = new TextLine();
        
        if (timeStamp) {
            textLine.appendText(getCurrentTimeStr(), FONT_TIME_STAMP, COLOR_TIME_STAMP, 0, 3, 2);
        }
        
        textLine.appendText(sender + ">", FONT_SENDER, COLOR_SENDER, 0, 3, 0);
        
        Color msgColor = COLOR_MESSAGE_1;
        Font msgFont = FONT_MESSAGE_1;
        
        if (reciever != null) {
//            textLine.appendText("[" + reciever + "]", FONT_SENDER, COLOR_SENDER, 0, 3, 0);
            msgColor = COLOR_MESSAGE_2;
            msgFont = FONT_MESSAGE_2;
        }
        
        Matcher itemLinkMatcher = ITEM_LINK_PATTERN.matcher(msg);
        Matcher httpLinkMatcher = HTTP_LINK_PATTERN.matcher(msg);
        int index = 0;
        boolean foundItemLink = itemLinkMatcher.find();
        boolean foundHttpLink = httpLinkMatcher.find();
        while (foundItemLink || foundHttpLink) {
            int start = 0;
            if (foundItemLink && foundHttpLink) {
                start = itemLinkMatcher.start() < httpLinkMatcher.start() ? itemLinkMatcher.start() : httpLinkMatcher.start();
            } else if (foundItemLink) {
                start = itemLinkMatcher.start();
            } else if (foundHttpLink) {
                start = httpLinkMatcher.start();
            }
            if (start > index) {
                textLine.appendText(msg.substring(index, start), msgFont, msgColor, 0, 0, 0);
            }
            if (foundItemLink && start == itemLinkMatcher.start()) {
                ChatLink chatLink = new ChatLink(msg.substring(itemLinkMatcher.start(), itemLinkMatcher.end()));
                ItemLink itemLink = new ItemLink(chatLink, 1, 1, 0);
                itemLink.getLabel().setForeground(COLOR_ITEM_LINK);
                textLine.addLinePart(itemLink);
                index = itemLinkMatcher.end();
                foundItemLink = itemLinkMatcher.find();
            } else if (foundHttpLink && start == httpLinkMatcher.start()) {
                HttpLink httpLink = new HttpLink(msg.substring(httpLinkMatcher.start(), httpLinkMatcher.end()), 1, 1, 0);
                httpLink.getLabel().setForeground(COLOR_WEB_LINK);
                textLine.addLinePart(httpLink);
                index = httpLinkMatcher.end();
                foundHttpLink = httpLinkMatcher.find();
            }
        }
        if (index < msg.length()) {
            textLine.appendText(msg.substring(index, msg.length()), msgFont, msgColor, 0, 0, 0);
        }

        return textLine;
    }
    
    private String getCurrentTimeStr() {
        long timeSecs = System.currentTimeMillis() / 1000;
        long timeMins = timeSecs / 60;
        long timeHours = timeMins / 60 + 1;
        int hours = (int)(timeHours % 24);
        int mins = (int)(timeMins % 60);
        
        StringBuilder timeStr = new StringBuilder();
        if (hours < 10) {
            timeStr.append('0');
        }
        timeStr.append(hours);
        timeStr.append(':');
        if (mins < 10) {
            timeStr.append('0');
        }
        timeStr.append(mins);
        
        return timeStr.toString();
    }
    
    @Override
    public void setTimeStamp(boolean b) {
        this.timeStamp = b;
    }

    @Override
    public boolean isTimeStamp() {
        return timeStamp;
    }

}
